package com.situ.student.servlet.subject;

import com.alibaba.fastjson2.JSON;
import com.situ.student.model.Student;
import com.situ.student.model.Subject;
import com.situ.student.model.search.StudentSearchBean;
import com.situ.student.service.SubjectService;
import com.situ.student.service.impl.SubjectServiceImpl;
import com.situ.student.util.BeanFactory;
import com.situ.student.util.Pagination;
import com.situ.student.util.ServletUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

@WebServlet("/admin/subject/*")
public class SubjectServlet extends HttpServlet {
    private SubjectService subjectService = BeanFactory.getBean(SubjectServiceImpl.class);
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getPathInfo();
        if ("/list".equals(action)) {
            req.getRequestDispatcher("/WEB-INF/jsp/subject/list.jsp").forward(req, resp);
        }
        if("/edit".equals(action)){
            String id = req.getParameter("id");
            req.setAttribute("id", id);
            req.getRequestDispatcher("/WEB-INF/jsp/subject/edit.jsp").forward(req, resp);
        }
        if("/id".equals( action)){
            String id = req.getParameter("id");
            Subject subject = subjectService.findById(Integer.parseInt(id));
            ServletUtils.renderJson(resp, Map.of("subject", subject));
        }
        if("/add".equals(action)){
            req.getRequestDispatcher("/WEB-INF/jsp/subject/add.jsp").forward(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getPathInfo();
        if("/list".equals( action)){
            list(req, resp);
        }
        if("/delete".equals(action)){
            delete(req, resp);
        }
        if("/add".equals(action)){
            save(req, resp);
        }
        if("/edit".equals(action)){
            edit(req, resp);
        }
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String page = req.getParameter("page");//页码
        String limit = req.getParameter("limit");//页面大小
        //前端提交的查询条件
        String subjectName = req.getParameter("name");

        //查询条件封装
        Subject condition = new Subject();
        if (StringUtils.hasText(subjectName)) {
            condition.setName(subjectName);
        }

        int iPage = 1;
        if (StringUtils.hasText(page)) {
            try {
                iPage = Integer.parseInt(page);
            } catch (NumberFormatException e) {
                //iPage = 1;
            }
        }

        int iLimit = 20;
        if (StringUtils.hasText(limit)) {
            try {
                iLimit = Integer.parseInt(limit);
            } catch (NumberFormatException e) {
                //iLimit = 20;
            }
        }

        Pagination pagination = new Pagination(iPage, iLimit);
        List<Subject> subjects = subjectService.findAll(pagination, condition);
        Map<String, Object> map = Map.of("subjects",subjects, "pi", pagination);
        //设置响应内容格式为json
        resp.setContentType("application/json;charset=UTF-8");
        //将java对象序列化成json字符串
        String json = JSON.toJSONString(map);
        PrintWriter out = resp.getWriter();
        out.write(json);
        out.flush();
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String ids = req.getParameter("ids");
        int rows = subjectService.deleteByIds(Integer.parseInt(ids));
        Map<String,Object> map = Map.of("rows", rows);
        ServletUtils.renderJson(resp, map);
    }

    private void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        String description = req.getParameter("description");

        Subject subject = new Subject();

        if (!StringUtils.hasText(name)) {
            ServletUtils.renderJson(resp, Map.of("error", "名称不可为空"));
            return;
        }

        subject.setName( name);
        subject.setDescription(description);


        //调用业务类
        boolean success = subjectService.save(subject);

        ServletUtils.renderJson(resp, Map.of("success", success));
    }

    //修改
    private void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String description = req.getParameter("description");

        Subject subject = new Subject();
        subject.setId(Integer.parseInt(id));

        if (!StringUtils.hasText(name)) {
            ServletUtils.renderJson(resp, Map.of("error", "名称不可为空"));
            return;
        }

        subject.setName(name);
        subject.setDescription(description);

        //调用业务类
        boolean success = subjectService.update(subject);

        ServletUtils.renderJson(resp, Map.of("success", success));
    }

}
